package cn.com.zcode.user.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.session.Configuration;

import cn.com.zcode.core.dao.BaseMybatisDaoSupport;
import cn.com.zcode.user.model.InviteCode;
import cn.com.zcode.user.model.LoginInfo;
import cn.com.zcode.user.model.User;

public class UserDaoImpl extends BaseMybatisDaoSupport implements UserDao {
	 

	public User getUser(String username, String password) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("username", username);
		map.put("password", password);
		User user = this.getSqlSession().selectOne("getUser", map);
		Configuration conf = this.getSqlSession().getConfiguration();
		ResultMap rm = conf.getResultMap("User");
		System.out.println(rm);
		return user;
	}

	@Override
	public User getUserById(String id) {
		User user = this.getSqlSession().selectOne("getUserById", id);
		return user;
	}

	@Override
	public User getUserByName(String name) {
		User user = this.getSqlSession().selectOne("getUserByName", name);
		return user;
	}

	@Override
	public boolean checkInviteCode(String codenum) {
		Integer a = this.getSqlSession().selectOne("checkInviteCode", codenum);
		return a != null && a > 0;

	}

	@Override
	public List<InviteCode> listInviteCodes() {
		List<InviteCode> list = this.getSqlSession().selectList("listInviteCodes");
		return list;
	}

	@Override
	public List<User> findUsers(String[] userid) {
		List<User> list = this.getSqlSession().selectList("findUsers", userid);
		return list;
	}

	@Override
	public void usedInviteCode(String uid, String code) {
		uid = uid.trim();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("uid", uid);
		map.put("usedTime", new Date());
		map.put("code", code);
		this.getSqlSession().update("usedInviteCode", map);
	}

	@Override
	public LoginInfo addLoginInfo(LoginInfo logininfo) {
		return this.add("addLoginInfo", logininfo);
	}

	@Override
	public User addUser(User user) {
		return this.add("addUser", user);
	}

	@Override
	public InviteCode addInviteCode(InviteCode code) {
		  return this.add("addInviteCode", code); 
	}

	@Override
	public List<User> listAllUsers() {
		return this.list("listAllUsers");
	}

}
